package com.kaoshi.user.config;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class UsernameHolder {
    private static final ThreadLocal<String> threadLocalUsername = new ThreadLocal<>();
    private static final ConcurrentMap<String, String> usernameMap = new ConcurrentHashMap<>();

    public static void setUsername(String username) {
        System.out.println("setUsername: "+username);
        threadLocalUsername.set(username);
        usernameMap.put(Thread.currentThread().getName(), username);
    }

    public static String getUsername() {
        System.out.println("getUsername: ");
        return threadLocalUsername.get();
    }

    public static void clear() {
        threadLocalUsername.remove();
        usernameMap.remove(Thread.currentThread().getName());
    }
}
